#include<my_header.h>

//1.定义共享资源
typedef struct Shared_s{
    int nums;
    pthread_mutex_t mutex;
    pthread_cond_t cond;
    int turn; 
    int isAdd;
}Shared_t;

void sell_one(Shared_t* share){
    Shared_t* p=share;
    if(p->nums==0){
        printf("火车票已经售完\n");
        return;
    }
    --p->nums;
    sleep(1);
    printf("卖出一张票,还剩%d张\n",p->nums);
}

void *func1(void* arg){
    //4转化指针
    Shared_t* p=(Shared_t*) arg;
    while(1){
        pthread_mutex_lock(&p->mutex);
        while(!(p->nums>0 && p->turn==1 &&(p->nums>=10||p->isAdd==1))){
            if(p->nums == 0){
                pthread_mutex_unlock(&p->mutex);
                break;
            }
            pthread_cond_wait(&p->cond, &p->mutex);
        }
        if(p->nums == 0){
            pthread_mutex_unlock(&p->mutex);
            break;
        }
        printf("%lu子线程开始卖票：",pthread_self());
        sell_one(p);
        p->turn = 0;
        if(p->nums == 0){
            pthread_cond_broadcast(&p->cond);
            pthread_mutex_unlock(&p->mutex);
            break;
        }
        pthread_cond_broadcast(&p->cond);
        pthread_mutex_unlock(&p->mutex);
    }
    return NULL;
}

void *func2(void* arg){
    //4转化指针
    Shared_t* p=(Shared_t*) arg;
    while(1){
        pthread_mutex_lock(&p->mutex);
        if(p->nums == 0){
            pthread_mutex_unlock(&p->mutex);
            break;
        }
        while(!(p->nums>0 && p->turn==0 &&(p->nums>=10||p->isAdd==1))){
            if(p->nums == 0){
                pthread_mutex_unlock(&p->mutex);
                break;
            }
            pthread_cond_wait(&p->cond, &p->mutex);
        }
        if(p->nums == 0){
            pthread_mutex_unlock(&p->mutex);
            break;
        }
        printf("%lu子线程开始卖票：",pthread_self());
        sell_one(p);
        p->turn = 1;
        if(p->nums == 0){
            pthread_cond_broadcast(&p->cond);
            pthread_mutex_unlock(&p->mutex);
            break;
        }
        pthread_cond_broadcast(&p->cond);
        pthread_mutex_unlock(&p->mutex);
    }
    return NULL;
}

int main(){
    //2.初始化共享资源
    Shared_t* share=(Shared_t*) calloc(1,sizeof(Shared_t));
    share->nums=20;
    pthread_mutex_init(&share->mutex,NULL);
    pthread_cond_init(&share->cond,NULL);
    share->turn = 0; 
    share->isAdd  = 0;

    //3.创建线程并初始化
    pthread_t tid1,tid2;
    pthread_create(&tid1,NULL,func1,share);
    pthread_create(&tid2,NULL,func2,share);

    //A事件任务
    while(1){
        pthread_mutex_lock(&share->mutex);
        
        if(share->nums == 0){
            pthread_cond_broadcast(&share->cond);
            pthread_mutex_unlock(&share->mutex);
            break;
        }

        // while(!((share->nums < 10 && share->isAdd == 0))){
        //     pthread_cond_wait(&share->cond,&share->mutex);
        // }

        // 修改：添加票数>0的条件
        while(!(share->nums < 10 && share->isAdd == 0) && share->nums > 0) {
            pthread_cond_wait(&share->cond, &share->mutex);
        }

        // 再次检查票数（可能被唤醒时已为0）
        if (share->nums == 0) {
            pthread_cond_broadcast(&share->cond);
            pthread_mutex_unlock(&share->mutex);
            break;
        }
        share->nums+=10;
        share->isAdd=1;
        printf("------------------------------------------\n");
        printf("补充10张火车票完毕!当前还有%d张票\n",share->nums);
        printf("------------------------------------------\n");
        pthread_cond_broadcast(&share->cond);
        pthread_mutex_unlock(&share->mutex);
        
    }

    pthread_join(tid1, NULL);
    pthread_join(tid2,NULL);
    pthread_cond_destroy(&share->cond);
    pthread_mutex_destroy(&share->mutex);
    free(share);
    printf("已经销售完全部火车票\n");
    return 0;
}